package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjRequestFundsPlanDao;
import cn.topevery.garden.entity.po.ProjRequestFundsPlan;
import cn.topevery.garden.entity.query.ProjRequestFundsPlanQuery;
import cn.topevery.garden.entity.vo.ProjRequestFundsPlanVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import hw.topevery.framework.entity.Value;
import hw.topevery.framework.enums.DbTypeEnum;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 项目请款计划表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Repository
public class ProjRequestFundsPlanDaoImpl extends BaseDaoImpl<ProjRequestFundsPlan> implements ProjRequestFundsPlanDao {

    @Override
    public <P extends DbCsp> List<ProjRequestFundsPlan> getSearch(P para) {
        return null;
    }

    public SqlQueryMate getListOrPageSqlQueryMate(ProjRequestFundsPlanQuery query) {
        String table = "t_proj_request_funds_plan tprfp";
        String columns = "tprfp.*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tprfp.c_db_create_date asc");
        sqlQueryMate.where(true, "tprfp.c_db_status", ScriptConditionEnum.Eq, 0);
        sqlQueryMate.where(ObjectUtil.isNotEmpty(query.getProjId()), "tprfp.c_proj_id", ScriptConditionEnum.Eq, query.getProjId());
        return sqlQueryMate;
    }

    @Override
    public List<ProjRequestFundsPlanVO> list(ProjRequestFundsPlanQuery query) {
        SqlQueryMate sqlQueryMate = getListOrPageSqlQueryMate(query);
        return selectList(sqlQueryMate, ProjRequestFundsPlanVO.class);
    }

    @Override
    public DataPager<ProjRequestFundsPlanVO> listPage(ProjRequestFundsPlanQuery query) {
        Integer pageIndex = query.pageIndex - 1;
        SqlQueryMate sqlQueryMate = getListOrPageSqlQueryMate(query);
        return getDataPager(sqlQueryMate, query.pageSize, pageIndex, ProjRequestFundsPlanVO.class);
    }

    @Override
    public ProjRequestFundsPlan getEntityByGuidId(String reqFundsId) {
        return getEntityByColumnEqValue(new KeyValue<>("c_req_funds_id", reqFundsId));
    }

    @Override
    public boolean updatePaymentMoneyByReqFundsId(String reqFundsId, BigDecimal paymentMoney) {
        Value<Boolean> res = new Value<>();
        String sql = " update t_proj_request_funds_plan tprfp " +
                " set tprfp.c_payment_money = :p_paymentMoney, " +
                "     tprfp.c_is_payment_complate = (case when c_req_funds_money <= :p_paymentMoney then 1 else 0 end) " +
                "where tprfp.c_req_funds_id = :p_reqFundsId";
        run(sql, CommandType.Text, dbCmd -> {
            dbCmd.addInParameter("p_paymentMoney", DbTypeEnum.DECIMAL, paymentMoney);
            dbCmd.addInParameter("p_reqFundsId", DbTypeEnum.VARCHAR, reqFundsId);
            res.data = dbCmd.executeNonQuery();
        });
        return res.data;
    }
}
